﻿using System.Data;
using System.Data.SqlClient;
using JCode.DBUtility;
using JCode.IDAL;
using System.Text;
using System;

namespace JCode.SQLiteDAL
{
   public class Category:ICategory
    {
        //添加
        public int Add(JCode.Model.CategoryInfo model)
        {

            SqlParameter[] parameters = {
					new SqlParameter("@ParentId", SqlDbType.Int,4),
					new SqlParameter("@CategoryName", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.ParentID;
            parameters[1].Value = model.CategoryName;
            SQLHelper.ExecNonQueryReturn_Proc("P_Add_Category", parameters);
            return (int)parameters[0].Value;
        }

        //获取列表

        public DataSet GetList()
        {
          
            return SQLite.GetDataSet("select * from Category");
        }

        public JCode.Model.CategoryInfo GetModel(int CategoryId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 CategoryID,CategoryName,ParentId,Layer,DisplayOrder,DateCreated from Category ");
            strSql.Append(" where CategoryID=@CategoryID ");
            SqlParameter[] parameters = {
					new SqlParameter("@CategoryID", SqlDbType.Int,4)};
            parameters[0].Value = CategoryId;

            JCode.Model.CategoryInfo model = new JCode.Model.CategoryInfo();

            DataSet ds = SQLHelper.GetDataSet_Sp(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["CategoryID"].ToString() != "")
                {
                    model.CategoryID = int.Parse(ds.Tables[0].Rows[0]["CategoryID"].ToString());
                }
                model.CategoryName = ds.Tables[0].Rows[0]["CategoryName"].ToString();
                if (ds.Tables[0].Rows[0]["ParentId"].ToString() != "")
                {
                    model.ParentID = int.Parse(ds.Tables[0].Rows[0]["ParentId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Layer"].ToString() != "")
                {
                    model.Layer = int.Parse(ds.Tables[0].Rows[0]["Layer"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DisplayOrder"].ToString() != "")
                {
                    model.DisplayOrder = int.Parse(ds.Tables[0].Rows[0]["DisplayOrder"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DateCreated"].ToString() != "")
                {
                    model.DateCreated = DateTime.Parse(ds.Tables[0].Rows[0]["DateCreated"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(JCode.Model.CategoryInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Category set ");
            strSql.Append("CategoryName=@CategoryName ");
            strSql.Append(" where CategoryID=@CategoryID ");
            SqlParameter[] parameters = {
					new SqlParameter("@CategoryID", SqlDbType.Int,4),
					new SqlParameter("@CategoryName", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.CategoryID;
            parameters[1].Value = model.CategoryName;

            SQLHelper.GetScalar_Sp(strSql.ToString(), parameters);
        }
    }
}
